import java.util.concurrent.locks.StampedLock;

public class Point {
    private double x;
    private double y;
    private final StampedLock stampedLock = new StampedLock();

    public void move(double deltaX, double deltaY) {
        long wLock = stampedLock.writeLock();
        try {
            this.x += deltaX;
            this.y += deltaY;
        } finally {
            stampedLock.unlock(wLock);
        }
    }

    public double getDistance() {
        long rLock = stampedLock.tryOptimisticRead();
        double x = this.x;
        double y = this.y;
        if (!stampedLock.validate(rLock)) {
            rLock= stampedLock.readLock();
            try {
               x = this.x;
               y = this.y;
            } finally {
                stampedLock.unlock(rLock);
            }
        }
        return Math.sqrt(x * x + y * y);
    }
}
